home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / UNZIP_1 / APPNOTE.TXT < prev    next >
Text File  |  1992-07-19  |  25KB  |  754 lines

  1. (tab width = 7 in this document)
  2.  
  3. Disclaimer
  4. ----------
  5.  
  6. Although PKWARE will attempt to supply current and accurate
  7. information relating to its file formats, algorithms, and the
  8. subject programs, the possibility of error can not be eliminated.
  9. PKWARE therefore expressly disclaims any warranty that the
  10. information contained in the associated materials relating to the
  11. subject programs and/or the format of the files created or
  12. accessed by the subject programs and/or the algorithms used by
  13. the subject programs, or any other matter, is current, correct or
  14. accurate as delivered.  Any risk of damage due to any possible
  15. inaccurate information is assumed by the user of the information.
  16. Furthermore, the information relating to the subject programs
  17. and/or the file formats created or accessed by the subject
  18. programs and/or the algorithms used by the subject programs is
  19. subject to change without notice.
  20.  
  21.  
  22. General Format of a ZIP file
  23. ----------------------------
  24.  
  25.   Files stored in arbitrary order.  Large zipfiles can span multiple
  26.   diskette media.
  27.  
  28.   Overall zipfile format:
  29.  
  30.     [local file header+file data] . . .
  31.     [central directory] end of central directory record
  32.  
  33.  
  34.   A.  Local file header:
  35.  
  36.     local file header signature        4 bytes  (0x04034b50)
  37.     version needed to extract        2 bytes
  38.     general purpose bit flag        2 bytes
  39.     compression method            2 bytes
  40.     last mod file time             2 bytes
  41.     last mod file date            2 bytes
  42.     crc-32                   4 bytes
  43.     compressed size            4 bytes
  44.     uncompressed size            4 bytes
  45.     filename length            2 bytes
  46.     extra field length            2 bytes
  47.  
  48.     filename (variable size)
  49.     extra field (variable size)
  50.  
  51.  
  52.   B.  Central directory structure:
  53.  
  54.       [file header] . . .  end of central dir record
  55.  
  56.       File header:
  57.  
  58.     central file header signature    4 bytes  (0x02014b50)
  59.     version made by            2 bytes
  60.     version needed to extract        2 bytes
  61.     general purpose bit flag        2 bytes
  62.     compression method            2 bytes
  63.     last mod file time             2 bytes
  64.     last mod file date            2 bytes
  65.     crc-32                   4 bytes
  66.     compressed size            4 bytes
  67.     uncompressed size            4 bytes
  68.     filename length            2 bytes
  69.     extra field length            2 bytes
  70.     file comment length            2 bytes
  71.     disk number start            2 bytes
  72.     internal file attributes        2 bytes
  73.     external file attributes        4 bytes
  74.     relative offset of local header    4 bytes
  75.  
  76.     filename (variable size)
  77.     extra field (variable size)
  78.     file comment (variable size)
  79.  
  80.       End of central dir record:
  81.  
  82.     end of central dir signature    4 bytes  (0x06054b50)
  83.     number of this disk            2 bytes
  84.     number of the disk with the
  85.     start of the central directory    2 bytes
  86.     total number of entries in
  87.     the central dir on this disk    2 bytes
  88.     total number of entries in
  89.     the central dir            2 bytes
  90.     size of the central directory       4 bytes
  91.     offset of start of central
  92.     directory with respect to
  93.     the starting disk number        4 bytes
  94.     zipfile comment length        2 bytes
  95.     zipfile comment (variable size)
  96.  
  97.  
  98.  
  99.  
  100.   C.  Explanation of fields:
  101.  
  102.       version made by
  103.  
  104.       The upper byte indicates the host system (OS) for the
  105.       file.  Software can use this information to determine
  106.       the line record format for text files etc.  The current
  107.       mappings are:
  108.  
  109.       0 - MS-DOS and OS/2 (F.A.T. file systems)
  110.       1 - Amiga    2 - VMS        3 - *nix    4 - VM/CMS
  111.       5 - Atari ST                  6 - OS/2 1.2 extended file systems
  112.       7 - Macintosh            8 thru 255 - unused
  113.  
  114.       The lower byte indicates the version number of the
  115.       software used to encode the file.  The value/10
  116.       indicates the major version number, and the value
  117.       mod 10 is the minor version number.
  118.  
  119.       version needed to extract
  120.  
  121.       The minimum software version needed to extract the
  122.       file, mapped as above.
  123.  
  124.       general purpose bit flag:
  125.  
  126.           bit 0: If set, indicates that the file is encrypted.
  127.           bit 1: If the compression method used was type 6,
  128.          Imploding, then this bit, if set, indicates
  129.          an 8K sliding dictionary was used.  If clear,
  130.          then a 4K sliding dictionary was used.
  131.           bit 2: If the compression method used was type 6,
  132.          Imploding, then this bit, if set, indicates
  133.          an 3 Shannon-Fano trees were used to encode the
  134.          sliding dictionary output.  If clear, then 2
  135.          Shannon-Fano trees were used.
  136.       Note:  Bits 1 and 2 are undefined if the compression
  137.          method is other than type 6 (Imploding).
  138.  
  139.           The upper three bits are reserved and used internally
  140.       by the software when processing the zipfile.  The
  141.       remaining bits are unused in version 1.0.
  142.  
  143.       compression method:
  144.  
  145.       (see accompanying documentation for algorithm
  146.       descriptions)
  147.  
  148.       0 - The file is stored (no compression)
  149.       1 - The file is Shrunk
  150.       2 - The file is Reduced with compression factor 1
  151.       3 - The file is Reduced with compression factor 2
  152.       4 - The file is Reduced with compression factor 3
  153.       5 - The file is Reduced with compression factor 4
  154.          6 - The file is Imploded
  155.  
  156.       date and time fields:
  157.  
  158.       The date and time are encoded in standard MS-DOS
  159.       format.
  160.  
  161.       CRC-32:
  162.  
  163.       The CRC-32 algorithm was generously contributed by
  164.       David Schwaderer and can be found in his excellent
  165.       book ╥C Programmers Guide to NetBIOS╙ published by
  166.       Howard W. Sams & Co. Inc.  The ╘magic number╒ for
  167.       the CRC is 0xdebb20e3.  The proper CRC pre and post
  168.       conditioning is used, meaning that the CRC register
  169.       is pre-conditioned with all ones (a starting value
  170.       of 0xffffffff) and the value is post-conditioned by
  171.       taking the one╒s complement of the CRC residual.
  172.     
  173.       compressed size:
  174.       uncompressed size:
  175.  
  176.       The size of the file compressed and uncompressed,
  177.       respectively.
  178.  
  179.       filename length:
  180.       extra field length:
  181.       file comment length:
  182.  
  183.       The length of the filename, extra field, and comment
  184.       fields respectively.  The combined length of any
  185.       directory record and these three fields should not
  186.       generally exceed 65,535 bytes.
  187.  
  188.       disk number start:
  189.  
  190.       The number of the disk on which this file begins.
  191.  
  192.       internal file attributes:
  193.  
  194.       The lowest bit of this field indicates, if set, that
  195.       the file is apparently an ASCII or text file.  If not
  196.       set, that the file apparently contains binary data.
  197.       The remaining bits are unused in version 1.0.
  198.  
  199.       external file attributes:
  200.  
  201.       The mapping of the external attributes is
  202.       host-system dependent (see ╘version made by╒).  For
  203.       MS-DOS, the low order byte is the MS-DOS directory
  204.       attribute byte.
  205.  
  206.       relative offset of local header:
  207.  
  208.       This is the offset from the start of the first disk on
  209.       which this file appears, to where the local header should
  210.       be found.
  211.  
  212.       filename:
  213.  
  214.       The name of the file, with optional relative path.
  215.       The path stored should not contain a drive or
  216.       device letter, or a leading slash.  All slashes
  217.       should be forward slashes ╘/╒ as opposed to
  218.       backwards slashes ╘\╒ for compatibility with Amiga
  219.       and Unix file systems etc.
  220.  
  221.       extra field:
  222.  
  223.       This is for future expansion.  If additional information
  224.       needs to be stored in the future, it should be stored
  225.       here.  Earlier versions of the software can then safely
  226.       skip this file, and find the next file or header.  This
  227.       field will be 0 length in version 1.0.
  228.  
  229.       In order to allow different programs and different types
  230.       of information to be stored in the ╘extra╒ field in .ZIP
  231.       files, the following structure should be used for all
  232.       programs storing data in this field:
  233.  
  234.       header1+data1 + header2+data2 . . .
  235.  
  236.       Each header should consist of:
  237.  
  238.         Header ID - 2 bytes
  239.         Data Size - 2 bytes
  240.  
  241.       Note: all fields stored in Intel low-byte/high-byte order.
  242.  
  243.       The Header ID field indicates the type of data that is in
  244.       the following data block.
  245.  
  246.       Header ID╒s of 0 thru 31 are reserved for use by PKWARE.
  247.       The remaining ID╒s can be used by third party vendors for
  248.       proprietary usage.
  249.  
  250.  
  251.       The Data Size field indicates the size of the following
  252.       data block. Programs can use this value to skip to the
  253.       next header block, passing over any data blocks that are
  254.       not of interest.
  255.  
  256.       Note: As stated in the standard PKZIP application notes,
  257.         the size of the entire .ZIP file header, including the
  258.         filename, comment, and extra field should not exceed 64K
  259.         in size.
  260.  
  261.  
  262.       In case two different programs should appropriate the same
  263.       Header ID value, it is strongly recommended that each
  264.       program place a unique signature of at least two bytes in
  265.       size (and preferably 4 bytes or bigger) at the start of
  266.       each data area.  Every program should verify that it╒s
  267.       unique signature is present, in addition to the Header ID
  268.       value being correct, before assuming that it is a block of
  269.       known type.
  270.  
  271.  
  272.       file comment:
  273.  
  274.       The comment for this file.
  275.  
  276.  
  277.       number of this disk:
  278.  
  279.       The number of this disk, which contains central
  280.       directory end record.
  281.  
  282.       number of the disk with the start of the central directory:
  283.  
  284.       The number of the disk on which the central
  285.       directory starts.
  286.  
  287.       total number of entries in the central dir on this disk:
  288.  
  289.       The number of central directory entries on this disk.
  290.     
  291.       total number of entries in the central dir:
  292.  
  293.       The total number of files in the zipfile.
  294.  
  295.  
  296.       size of the central directory:
  297.  
  298.       The size (in bytes) of the entire central directory.
  299.  
  300.       offset of start of central directory with respect to
  301.       the starting disk number:
  302.  
  303.       Offset of the start of the central direcory on the
  304.       disk on which the central directory starts.
  305.  
  306.       zipfile comment length:
  307.  
  308.       The length of the comment for this zipfile.
  309.  
  310.       zipfile comment:
  311.  
  312.       The comment for this zipfile.
  313.  
  314.  
  315.   D.  General notes:
  316.  
  317.       1)  All fields unless otherwise noted are unsigned and stored
  318.       in Intel low-byte:high-byte, low-word:high-word order.
  319.  
  320.       2)  String fields are not null terminated, since the
  321.       length is given explicitly.
  322.  
  323.       3)  Local headers should not span disk boundries.  Also, even
  324.       though the central directory can span disk boundries, no
  325.       single record in the central directory should be split
  326.       across disks.
  327.  
  328.       4)  The entries in the central directory may not necessarily
  329.       be in the same order that files appear in the zipfile.
  330.  
  331. UnShrinking
  332. -----------
  333.  
  334. Shrinking is a Dynamic Ziv-Lempel-Welch compression algorithm
  335. with partial clearing.  The initial code size is 9 bits, and
  336. the maximum code size is 13 bits.  Shrinking differs from
  337. conventional Dynamic Ziv-lempel-Welch implementations in several
  338. respects:
  339.  
  340. 1)  The code size is controlled by the compressor, and is not
  341.     automatically increased when codes larger than the current
  342.     code size are created (but not necessarily used).  When
  343.     the decompressor encounters the code sequence 256
  344.     (decimal) followed by 1, it should increase the code size
  345.     read from the input stream to the next bit size.  No
  346.     blocking of the codes is performed, so the next code at
  347.     the increased size should be read from the input stream
  348.     immediately after where the previous code at the smaller
  349.     bit size was read.  Again, the decompressor should not
  350.     increase the code size used until the sequence 256,1 is
  351.     encountered.
  352.  
  353. 2)  When the table becomes full, total clearing is not
  354.     performed.  Rather, when the compresser emits the code
  355.     sequence 256,2 (decimal), the decompressor should clear
  356.     all leaf nodes from the Ziv-Lempel tree, and continue to
  357.     use the current code size.  The nodes that are cleared
  358.     from the Ziv-Lempel tree are then re-used, with the lowest
  359.     code value re-used first, and the highest code value
  360.     re-used last.  The compressor can emit the sequence 256,2
  361.     at any time.
  362.  
  363.  
  364.  
  365. Expanding
  366. ---------
  367.  
  368. The Reducing algorithm is actually a combination of two
  369. distinct algorithms.  The first algorithm compresses repeated
  370. byte sequences, and the second algorithm takes the compressed
  371. stream from the first algorithm and applies a probabilistic
  372. compression method.
  373.  
  374. The probabilistic compression stores an array of ╘follower
  375. sets╒ S(j), for j=0 to 255, corresponding to each possible
  376. ASCII character.  Each set contains between 0 and 32
  377. characters, to be denoted as S(j)[0],╔,S(j)[m], where m<32.
  378. The sets are stored at the beginning of the data area for a
  379. Reduced file, in reverse order, with S(255) first, and S(0)
  380. last.
  381.  
  382. The sets are encoded as { N(j), S(j)[0],╔,S(j)[N(j)-1] },
  383. where N(j) is the size of set S(j).  N(j) can be 0, in which
  384. case the follower set for S(j) is empty.  Each N(j) value is
  385. encoded in 6 bits, followed by N(j) eight bit character values
  386. corresponding to S(j)[0] to S(j)[N(j)-1] respectively.  If
  387. N(j) is 0, then no values for S(j) are stored, and the value
  388. for N(j-1) immediately follows.
  389.  
  390. Immediately after the follower sets, is the compressed data
  391. stream.  The compressed data stream can be interpreted for the
  392. probabilistic decompression as follows:
  393.  
  394.  
  395. let Last-Character <- 0.
  396. loop until done
  397.     if the follower set S(Last-Character) is empty then
  398.     read 8 bits from the input stream, and copy this
  399.     value to the output stream.
  400.     otherwise if the follower set S(Last-Character) is non-empty then
  401.     read 1 bit from the input stream.
  402.     if this bit is not zero then
  403.         read 8 bits from the input stream, and copy this
  404.         value to the output stream.
  405.     otherwise if this bit is zero then
  406.         read B(N(Last-Character)) bits from the input
  407.         stream, and assign this value to I.
  408.         Copy the value of S(Last-Character)[I] to the
  409.         output stream.
  410.     
  411.     assign the last value placed on the output stream to
  412.     Last-Character.
  413. end loop
  414.  
  415.  
  416. B(N(j)) is defined as the minimal number of bits required to
  417. encode the value N(j)-1.
  418.  
  419.  
  420. The decompressed stream from above can then be expanded to
  421. re-create the original file as follows:
  422.  
  423.  
  424. let State <- 0.
  425.  
  426. loop until done
  427.     read 8 bits from the input stream into C.
  428.     case State of
  429.     0:  if C is not equal to DLE (144 decimal) then
  430.         copy C to the output stream.
  431.         otherwise if C is equal to DLE then
  432.         let State <- 1.
  433.  
  434.     1:  if C is non-zero then
  435.         let V <- C.
  436.         let Len <- L(V)
  437.         let State <- F(Len).
  438.         otherwise if C is zero then
  439.         copy the value 144 (decimal) to the output stream.
  440.         let State <- 0
  441.  
  442.     2:  let Len <- Len + C
  443.         let State <- 3.
  444.  
  445.     3:  move backwards D(V,C) bytes in the output stream
  446.         (if this position is before the start of the output
  447.         stream, then assume that all the data before the
  448.         start of the output stream is filled with zeros).
  449.         copy Len+3 bytes from this position to the output stream.
  450.         let State <- 0.
  451.     end case
  452. end loop
  453.  
  454.  
  455. The functions F,L, and D are dependent on the ╘compression
  456. factor╒, 1 through 4, and are defined as follows:
  457.  
  458. For compression factor 1:
  459.     L(X) equals the lower 7 bits of X.
  460.     F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  461.     D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  462. For compression factor 2:
  463.     L(X) equals the lower 6 bits of X.
  464.     F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  465.     D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  466. For compression factor 3:
  467.     L(X) equals the lower 5 bits of X.
  468.     F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  469.     D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  470. For compression factor 4:
  471.     L(X) equals the lower 4 bits of X.
  472.     F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  473.     D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  474.  
  475.  
  476. Imploding
  477. ---------
  478.  
  479. The Imploding algorithm is actually a combination of two distinct
  480. algorithms.  The first algorithm compresses repeated byte
  481. sequences using a sliding dictionary.  The second algorithm is
  482. used to compress the encoding of the sliding dictionary ouput,
  483. using multiple Shannon-Fano trees.
  484.  
  485. The Imploding algorithm can use a 4K or 8K sliding dictionary
  486. size. The dictionary size used can be determined by bit 1 in the
  487. general purpose flag word, a 0 bit indicates a 4K dictionary
  488. while a 1 bit indicates an 8K dictionary.
  489.  
  490. The Shannon-Fano trees are stored at the start of the compressed
  491. file. The number of trees stored is defined by bit 2 in the
  492. general purpose flag word, a 0 bit indicates two trees stored, a
  493. 1 bit indicates three trees are stored.  If 3 trees are stored,
  494. the first Shannon-Fano tree represents the encoding of the
  495. Literal characters, the second tree represents the encoding of
  496. the Length information, the third represents the encoding of the
  497. Distance information.  When 2 Shannon-Fano trees are stored, the
  498. Length tree is stored first, followed by the Distance tree.
  499.  
  500. The Literal Shannon-Fano tree, if present is used to represent
  501. the entire ASCII character set, and contains 256 values.  This
  502. tree is used to compress any data not compressed by the sliding
  503. dictionary algorithm.  When this tree is present, the Minimum
  504. Match Length for the sliding dictionary is 3.  If this tree is
  505. not present, the Minimum Match Length is 2.
  506.  
  507. The Length Shannon-Fano tree is used to compress the Length part
  508. of the (length,distance) pairs from the sliding dictionary
  509. output. The Length tree contains 64 values, ranging from the
  510. Minimum Match Length, to 63 plus the Minimum Match Length.
  511.  
  512. The Distance Shannon-Fano tree is used to compress the Distance
  513. part of the (length,distance) pairs from the sliding dictionary
  514. output. The Distance tree contains 64 values, ranging from 0 to
  515. 63, representing the upper 6 bits of the distance value.  The
  516. distance values themselves will be between 0 and the sliding
  517. dictionary size, either 4K or 8K.
  518.  
  519. The Shannon-Fano trees themselves are stored in a compressed
  520. format. The first byte of the tree data represents the number of
  521. bytes of data representing the (compressed) Shannon-Fano tree
  522. minus 1.  The remaining bytes represent the Shannon-Fano tree
  523. data encoded as:
  524.  
  525.     High 4 bits: Number of values at this bit length + 1. (1 - 16)
  526.     Low  4 bits: Bit Length needed to represent value + 1. (1 - 16)
  527.  
  528. The Shannon-Fano codes can be constructed from the bit lengths
  529. using the following algorithm:
  530.  
  531. 1)  Sort the Bit Lengths in ascending order, while retaining the
  532.     order of the original lengths stored in the file.
  533.  
  534. 2)  Generate the Shannon-Fano trees:
  535.  
  536.     Code <- 0
  537.     CodeIncrement <- 0
  538.     LastBitLength <- 0
  539.     i <- number of Shannon-Fano codes - 1   (either 255 or 63)
  540.  
  541.     loop while i >= 0
  542.     Code = Code + CodeIncrement
  543.     if BitLength(i) <> LastBitLength then
  544.         LastBitLength=BitLength(i)
  545.         CodeIncrement = 1 shifted left (16 - LastBitLength)
  546.     ShannonCode(i) = Code
  547.     i <- i - 1
  548.     end loop
  549.  
  550.  
  551. 3)  Reverse the order of all the bits in the above ShannonCode()
  552.     vector, so that the most significant bit becomes the least
  553.     significant bit.  For example, the value 0x1234 (hex) would
  554.     become 0x2C48 (hex).
  555.  
  556. 4)  Restore the order of Shannon-Fano codes as originally stored
  557.     within the file.
  558.  
  559. Example:
  560.  
  561.     This example will show the encoding of a Shannon-Fano tree
  562.     of size 8.  Notice that the actual Shannon-Fano trees used
  563.     for Imploding are either 64 or 256 entries in size.
  564.  
  565. Example:   0x02, 0x42, 0x01, 0x13
  566.  
  567.     The first byte indicates 3 values in this table.  Decoding the
  568.     bytes:
  569.         0x42 = 5 codes of 3 bits long
  570.         0x01 = 1 code  of 2 bits long
  571.         0x13 = 2 codes of 4 bits long
  572.  
  573.     This would generate the original bit length array of:
  574.     (3, 3, 3, 3, 3, 2, 4, 4)
  575.  
  576.     There are 8 codes in this table for the values 0 thru 7.  Using the
  577.     algorithm to obtain the Shannon-Fano codes produces:
  578.  
  579.                                   Reversed     Order     Original
  580. Val  Sorted   Constructed Code      Value     Restored    Length
  581. ---  ------   -----------------   --------    --------    ------
  582. 0:     2      1100000000000000        11       101          3
  583. 1:     3      1010000000000000       101       001          3
  584. 2:     3      1000000000000000       001       110          3
  585. 3:     3      0110000000000000       110       010          3
  586. 4:     3      0100000000000000       010       100          3
  587. 5:     3      0010000000000000       100        11          2
  588. 6:     4      0001000000000000      1000      1000          4
  589. 7:     4      0000000000000000      0000      0000          4
  590.  
  591.  
  592. The values in the Val, Order Restored and Original Length columns
  593. now represent the Shannon-Fano encoding tree that can be used for
  594. decoding the Shannon-Fano encoded data.  How to parse the
  595. variable length Shannon-Fano values from the data stream is beyond the
  596. scope of this document.  (See the references listed at the end of
  597. this document for more information.)  However, traditional decoding
  598. schemes used for Huffman variable length decoding, such as the
  599. Greenlaw algorithm, can be succesfully applied.
  600.  
  601. The compressed data stream begins immediately after the
  602. compressed Shannon-Fano data.  The compressed data stream can be
  603. interpreted as follows:
  604.  
  605. loop until done
  606.     read 1 bit from input stream.
  607.  
  608.     if this bit is non-zero then       (encoded data is literal data)
  609.     if Literal Shannon-Fano tree is present
  610.         read and decode character using Literal Shannon-Fano tree.
  611.     otherwise
  612.         read 8 bits from input stream.
  613.     copy character to the output stream.
  614.     otherwise                   (encoded data is sliding dictionary match)
  615.     if 8K dictionary size
  616.         read 7 bits for offset Distance (lower 7 bits of offset).
  617.     otherwise
  618.         read 6 bits for offset Distance (lower 6 bits of offset).
  619.     
  620.     using the Distance Shannon-Fano tree, read and decode the
  621.       upper 6 bits of the Distance value.
  622.  
  623.     using the Length Shannon-Fano tree, read and decode
  624.       the Length value.
  625.     
  626.     Length <- Length + Minimum Match Length
  627.     
  628.     if Length = 63 + Minimum Match Length
  629.         read 8 bits from the input stream,
  630.         add this value to Length.
  631.  
  632.     move backwards Distance+1 bytes in the output stream, and
  633.     copy Length characters from this position to the output
  634.     stream.  (if this position is before the start of the output
  635.     stream, then assume that all the data before the start of
  636.     the output stream is filled with zeros).
  637. end loop
  638.  
  639. Decryption
  640. ----------
  641.  
  642. The encryption used in PKZIP was generously supplied by Roger
  643. Schlafly.  PKWARE is grateful to Mr. Schlafly for his expert
  644. help and advice in the field of data encryption.
  645.  
  646. PKZIP encrypts the compressed data stream.  Encrypted files must
  647. be decrypted before they can be extracted.
  648.  
  649. Each encrypted file has an extra 12 bytes stored at the start of
  650. the data area defining the encryption header for that file.  The
  651. encryption header is originally set to random values, and then
  652. itself encrypted, using 3, 32-bit keys.  The key values are
  653. initialized using the supplied encryption password.  After each byte
  654. is encrypted, the keys are then updated using psuedo-random number
  655. generation techniques in combination with the same CRC-32 algorithm
  656. used in PKZIP and described elsewhere in this document.
  657.  
  658. The following is the basic steps required to decrypt a file:
  659.  
  660. 1) Initialize the three 32-bit keys with the password.
  661. 2) Read and decrypt the 12-byte encryption header, further
  662.    initializing the encryption keys.
  663. 3) Read and decrypt the compressed data stream using the
  664.    encryption keys.
  665.  
  666.  
  667. Step 1 - Initializing the encryption keys
  668. -----------------------------------------
  669.  
  670. Key(0) <- 305419896
  671. Key(1) <- 591751049
  672. Key(2) <- 878082192
  673.  
  674. loop for i <- 0 to length(password)-1
  675.     update_keys(password(i))
  676. end loop
  677.  
  678.  
  679. Where update_keys() is defined as:
  680.  
  681.  
  682. update_keys(char):
  683.   Key(0) <- crc32(key(0),char)
  684.   Key(1) <- Key(1) + (Key(0) & 000000ffH)
  685.   Key(1) <- Key(1) * 134775813 + 1
  686.   Key(2) <- crc32(key(2),key(1) >> 24)
  687. end update_keys
  688.  
  689.  
  690. Where crc32(old_crc,char) is a routine that given a CRC value and a
  691. character, returns an updated CRC value after applying the CRC-32
  692. algorithm described elsewhere in this document.
  693.  
  694.  
  695. Step 2 - Decrypting the encryption header
  696. -----------------------------------------
  697.  
  698. The purpose of this step is to further initialize the encryption
  699. keys, based on random data, to render a plaintext attack on the
  700. data ineffective.
  701.  
  702.  
  703. Read the 12-byte encryption header into Buffer, in locations
  704. Buffer(0) thru Buffer(11).
  705.  
  706. loop for i <- 0 to 11
  707.     C <- buffer(i) ^ decrypt_byte()
  708.     update_keys(C)
  709.     buffer(i) <- C
  710. end loop
  711.  
  712.  
  713. Where decrypt_byte() is defined as:
  714.  
  715.  
  716. unsigned char decrypt_byte()
  717.     local unsigned short temp
  718.     temp <- Key(2) | 2
  719.     decrypt_byte <- (temp * (temp ^ 1)) >> 8
  720. end decrypt_byte
  721.  
  722.  
  723. After the header is decrypted, the last two bytes in Buffer
  724. should be the high-order word of the CRC for the file being
  725. decrypted, stored in Intel low-byte/high-byte order.  This can
  726. be used to test if the password supplied is correct or not.
  727.  
  728.  
  729. Step 3 - Decrypting the compressed data stream
  730. ----------------------------------------------
  731.  
  732. The compressed data stream can be decrypted as follows:
  733.  
  734.  
  735. loop until done
  736.     read a charcter into C
  737.     Temp <- C ^ decrypt_byte()
  738.     update_keys(temp)
  739.     output Temp
  740. end loop
  741.  
  742.  
  743.  
  744. References:
  745.  
  746.     Storer, James A. ╥Data Compression, Methods and Theory╙,
  747.        Computer Science Press, 1988
  748.  
  749.     Held, Gilbert  ╥Data Compression, Techniques and Applications,
  750.     Hardware and Software Considerations╙
  751.        John Wiley & Sons, 1987
  752.  
  753.  
  754.